<?php
namespace app\controller;

use app\BaseController;
use think\facade\Request;
use Http;
use app\model\UserModel;
use think\facade\Db;
class Wx extends BaseController
{

    /**
     * 获取小程序openid
     */
    public function getOpenid()
    {
        $jscode=Request::param('jscode');//小程序登录code
        $appid=env('WX.AppID');
        $appsecret=env('WX.AppSecret');
        $url="https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$appsecret&js_code=$jscode&grant_type=authorization_code ";
        $res=Http::get($url);
        $res=json_decode($res,true);
        $info = $this->wxLogin($res['openid']);
        return json($info);
        // return json($res);
    }


    //获取头像并保存
    public function getAvatar(){
        $file = $_FILES["file"];
        if ($file==null) {
            return json(array('code'=>1, 'msg'=>'未上传图片')   );
        }
        // 获取文件后缀
        $temp = explode(".", $_FILES["file"]["name"]);
        $extension = end($temp);
        // 判断文件是否合法
        if(!in_array($extension, array("gif","jpeg","jpg","png"))){
            return json(array('code'=>1, 'msg'=>'上传图片不合法'));
        }
        //创建文件夹获取当前年月日
        $folder="/upload/avatar/".date('Y-m-d');

        if(!is_dir('.'.$folder)){
            mkdir('.'.$folder,0777);//创建年+月的文件夹权限为最高
        }
        $filename=GetRandStr(6).'.'.$extension;
        $info = move_uploaded_file($file['tmp_name'], '.'.$folder.'/'.$filename); //移动文件到指定目录 建议文件名自己在修改一下
        $img = $folder.'/'.$filename;
        // exit(json_encode(array('code'=>0, 'msg'=>$img)));

        $model = new UserModel;
        $user_id = Request::param('user_id');
        $res = $model->where('id',$user_id)->save(['id'=>$user_id,'avatar'=>$img]);
        // $img="/upload/avatar/2023-09-21/q9s5tj.jpg";
        return json(array('code'=>0, 'msg'=>$img));
    }

    //登录并保存用户信息
    public function wxLogin($openid){
        //获取到微信openid自动为其分配昵称和头像
        //先查是否有此openid
        $model = new UserModel;
        $info=$model->where('openid',$openid)->find();
        if($info){
            return $info;
        }else{
            $data['openid']=$openid;
            $data['nickname']='微信用户'.GetRandStr(4);
            $data['created_time']=date('Y-m-d H:i:s');
            $save=$model->save($data);
            if($save){
                return $data;
            }
        }
    }

    //修改昵称
    public function changeNickname(){
        $nickname=Request::param('nickname');
        $user_id=Request::param('user_id');
        // halt($nickname);
        $model = new UserModel;
        $save = $model->where('id',$user_id)->save(['id'=>$user_id,'nickname'=>$nickname]);
        if($save!==false){
            return json(['code'=>200,'msg'=>'修改成功']);
        }
    }


    

    //登录并保存用户信息
    public function test(){
        //获取到微信openid自动为其分配昵称和头像
        //先查是否有此openid
        $model = new UserModel;
        $info=$model->where('id', 1)->find();
        // $info=Db::name('user')->find();
        dump($info);
    }

}
